package cloud.kiko.psn.user.service;


import cloud.kiko.psn.pojo.common.biz.User;
import cloud.kiko.psn.user.mapper.UserMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * on 2023/2/12 23:32
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    private static final String userCacheName = "userCache";

    @Cacheable(value = userCacheName, key = "#id")
    public User getUserById(long id) {
        return userMapper.findById(id);
    }

    @CachePut(value = userCacheName, key = "#user.id")
    public User updateUser(User user) {
        if (userMapper.updateUser(user)) {
            return user;
        }
        return null;
    }

    @CacheEvict(value = userCacheName, key = "#id")
    public boolean clearCachedUser(long id) {
        return true;
    }


    public boolean deleteUserById(long id) {
        return userMapper.deleteUserById(id);
    }
}
